package a.oschina.eventbusdemo.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import a.oschina.eventbusdemo.MessageEvent;
import a.oschina.eventbusdemo.R;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        showContentTv= ((TextView) findViewById(R.id.show_content_tv));
        EventBus.getDefault().register(this);
    }

    private TextView showContentTv;

    public void downloadPage(View view) {
        startActivityForResult(new Intent(this,SecondActivity.class),0);
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        showContentTv.setText(data.getStringExtra("content"));
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showContent(MessageEvent messageEvent){

        //ThreadMode.POSTING  订阅处理的线程和事件产生位于同一线程中
        //  Only the original thread that created a view hierarchy can touch its views.
        showContentTv.setText(messageEvent.getMessage());

        Log.i("TAG","Activity A");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }



}
